<script setup>
import {onMounted, ref} from "vue";
import {ElMessage} from "element-plus";
import axios from "axios";
import http from "../../http";  
import { useRoute, useRouter } from 'vue-router'
const router = useRouter()
const route = useRoute()
let resource_form = ref({
  res1:"",
  res2:"",
})


let resource_list=ref([])
const get_resource=()=>{
  http.get(`/app/resource/get_pid_notnull/`)
  .then(res=>{
    console.log(res)
    resource_list.value=res.data.data
  }
  )
}
const select1=(id)=>{
  http.get(`/app/resource/${id}/`)
  .then(res=>{
    console.log(res)
    resource_form.value.res1=res.data.id
  })
}
const select2=(id)=>{
  http.get(`/app/resource/${id}/`)
  .then(res=>{
    console.log(res)
    // resource_form.value.res2=res.data.name
    console.log(resource_form.value.res1)
    console.log(res.data.id)
    http.get(`/app/resource_exp/is_simple/`,{params:{res1:resource_form.value.res1,res2:res.data.id}})
      .then(res=>{
        console.log(res)
        if (res.data.msg == "不能选择相同的资源"){
          ElMessage.error("不能选择相同的资源")
          resource_form.value.res2=""
        }
        if(res.data.msg == "互斥关系已存在"){
          ElMessage.error("互斥关系已存在")
          resource_form.value.res2=""
        }
        if(res.data.msg == "可以添加数据"){
          ElMessage.success('可以添加数据')
          resource_form.value.res2=res.data.data
          console.log(resource_form.value)
        }
      }
    )
  })
}
const put_res_exp=()=>{
  console.log("===================")
  console.log(resource_form.value)
  http.post(`/app/resource_exp/`,resource_form.value)
  .then(res=>{
    console.log(res)
    ElMessage.success("添加成功")
    resource_form.value={
      res1:"",
      res2:""
    }
  })
}
onMounted(()=>{
  get_resource()
})
</script>

<template>
  <el-form :model="resource_form" label-width="auto" style="max-width: 600px">
  <el-form-item label="资源1">
      <el-select v-model="resource_form.res1" placeholder="请选择">
        <el-option v-for="item,key in resource_list" :key="key" :label="item.name" :value="item.id" @click="select1(item.id)"/>
      </el-select>
    </el-form-item>
  <el-form-item label="资源1">
      <el-select v-model="resource_form.res2" placeholder="请选择">
        <el-option v-for="item,key in resource_list" :key="key" :label="item.name" :value="item.id" @click="select2(item.id)"/>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="put_res_exp">添加</el-button>
      <el-button>取消</el-button>
    </el-form-item>
  </el-form>
   
</template>

<style scoped>

</style>